﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Wicresoft.RDW.Presentation;
using Wicresoft.RDW.Model;

namespace Wicresoft.RDW.Manage
{
    public partial class LocationManage : BasePage_BackEnd
    {
        protected void repLocation_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            var location = (e.Item.FindControl("lblLocation") as Label).Text;
            var locationId = LocationPresent.GetByCondition(g => g.Name_CN == location).LocationId;

            var ddlRole = e.Item.FindControl("ddlRole") as DropDownList;
            var role = RolePresent.GetList();
            ddlRole.DataSource = role;
            ddlRole.DataTextField = "Name";
            ddlRole.DataValueField = "RoleId";
            ddlRole.DataBind();

            var siteManager = SiteManagerPresent.GetByCondition(g => g.LocationId == locationId);
            if (siteManager != null)
                ddlRole.SelectedValue = siteManager.RoleId.ToString();
        }

        protected void repLab_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            var lab = (e.Item.FindControl("lblLab") as Label).Text;
            int labId = LabPresent.GetByCondition(g => g.Name == lab).LabId;

            var ddlRole = e.Item.FindControl("ddlRole") as DropDownList;
            var role = RolePresent.GetList();
            ddlRole.DataSource = role;
            ddlRole.DataTextField = "Name";
            ddlRole.DataValueField = "RoleId";
            ddlRole.DataBind();

            var labManager = LabManagerPresent.GetByCondition(g => g.LabId == labId);
            if (labManager != null)
                ddlRole.SelectedValue = labManager.RoleId.ToString();
        }

        protected void btnSave_OnClick(object sender, EventArgs e)
        {
            for (int i = 0; i < repLocation.Items.Count; i++)
            {
                var ddlRole = repLocation.Items[i].FindControl("ddlRole") as DropDownList;
                int roleId = int.Parse(ddlRole.SelectedValue);
                var location = repLocation.Items[i].FindControl("lblLocation") as Label;
                int locationId = LocationPresent.GetByCondition(g => g.Name_CN == location.Text).LocationId;
                var siteManager = SiteManagerPresent.GetByCondition(g => g.LocationId == locationId);
                if (siteManager == null)
                {
                    var newSitManager = new SiteManager() { RoleId = roleId, LocationId = locationId };
                    SiteManagerPresent.Create(newSitManager);
                }
                else
                {
                    siteManager.RoleId = roleId;
                    SiteManagerPresent.Update(siteManager);
                }
            }
            for (int i = 0; i < repLab.Items.Count; i++)
            {
                var ddlRole = repLab.Items[i].FindControl("ddlRole") as DropDownList;
                int roleId = int.Parse(ddlRole.SelectedValue);
                var lab = repLab.Items[i].FindControl("lblLab") as Label;
                int labId = LabPresent.GetByCondition(g => g.Name == lab.Text).LabId;
                var labManager = LabManagerPresent.GetByCondition(g => g.LabId == labId);
                if (labManager == null)
                {
                    var newLabManager = new LabManager() { RoleId = roleId, LabId = labId };
                    LabManagerPresent.Create(newLabManager);
                }
                else
                {
                    labManager.RoleId = roleId;
                    LabManagerPresent.Update(labManager);
                }
            }
        }

        protected void btnCancle_OnClick(object sender, EventArgs e)
        {
            ddlDataSource();
        }

        public override void InitControlResource()
        {
            lbtnSave.Text = Resources.NameResource.btnSubmit;
            lbtnCancle.Text = Resources.NameResource.btnCancel;

            ddlDataSource();
        }

        public void ddlDataSource()
        {
            repLocation.DataSource = LocationPresent.GetList();
            repLocation.DataBind();

            repLab.DataSource = LabPresent.GetList();
            repLab.DataBind();
        }
    }
}